<template>
  <div class="app-container">
    <el-dialog title="选择优惠券" :visible.sync="visible" width="1200px" append-to-body @close="toCancelBox">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px"
        @keyup.enter.native="handleQuery">
        <el-row>
          <el-form-item label="优惠券名称" prop="couponName">
            <el-input v-model="queryParams.couponName" placeholder="请输入优惠券名称" clearable size="small" class="input-rule" />
          </el-form-item>
          <el-form-item label="类型" prop="couponType">
            <el-select v-model="queryParams.couponType" placeholder="请选择类型" clearable size="small">
              <el-option v-for="dict in couponTypeOptions" :key="dict.dictValue" :label="dict.dictLabel"
                :value="dict.dictValue" />
            </el-select>
          </el-form-item>

          <el-form-item style="margin-left: 50px;">
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-row>
      </el-form>

      <!-- <el-row :gutter="10" class="row-mb2">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" :disabled="single">添加
          </el-button>
        </el-col>
      </el-row> -->

      <el-table v-loading="loading" ref="indexTable" :data="infoList2" @selection-change="handleSelectionChange" :row-key="bindRowKeys" border tooltip-effect="dark">
        <el-table-column type="selection" width="55" align="center" :reserve-selection="true" />
        <el-table-column label="图片" align="center" prop="couponIconUrl" width="80">
          <template slot-scope="scope">
            <el-image :z-index="zIndex" v-if="scope.row.couponIconUrl != null && scope.row.couponIconUrl != '' "
              slot="reference" :src="scope.row.couponIconUrl" class="image-rows"
              :preview-src-list="[scope.row.couponIconUrl]">
            </el-image>
          </template>
        </el-table-column>
        <el-table-column label="优惠券名称" align="center" prop="couponName" :show-overflow-tooltip="true" width="150"/>
        <el-table-column label="优惠券类型" align="center" prop="couponTypeName"  :show-overflow-tooltip="true" />
        <el-table-column label="优惠内容" align="center" prop="couponAmount" :show-overflow-tooltip="true" width="150">
          <template slot-scope="scope">
              <span>{{ scope.row.amountStr }}</span>
            </template>
          </el-table-column>
        <el-table-column label="使用门槛" align="center" prop="baseAmountStr" :show-overflow-tooltip="true" />
        <el-table-column label="发放总量" align="center" prop="couponNums" :show-overflow-tooltip="true" />
        <el-table-column label="已领用数量" align="center" prop="useNums" :show-overflow-tooltip="true" width="100"/>
        <el-table-column label="更新时间" align="center" prop="updateTime" width="200" sortable>
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span><br />
            <span>{{ scope.row.couponStatusStr }}</span>
          </template>
        </el-table-column>

      </el-table>

      <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList"  :page-sizes="pageSizess"/>

      <span slot="footer" class="dialog-footer">
        <el-button @click="determine" type="primary">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listCoupon,
    getCoupon,
    delCoupon,
    addCoupon,
    updateCoupon
  } from "@/api/activity/coupon";

  export default {
    name: "coupon",
    props: {
      flag: {
        type: Boolean,
      },
    },
    data() {
      return {
        pageSizess: [5, 10, 15, 20, 25, 30, 50],
        zIndex: 3000,
        // 遮罩层
        loading: true,

        datas: null,

        // 多选框传值
        idsHistory: [],
        checkIds: [],

        //商品
        selectedDatas: [],

        // 优惠券信息表格数据
        infoList2: [],
        infoListHistory: [],
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          couponName: null,
          couponType: null,
          couponStatus: '1',
        },
        // 表单参数
        form: {},
        multipleSelection2: [],
        // 类型字典
        couponTypeOptions: [],
        visible: false,
      };
    },
    created() {
      this.visible = this.flag;

      this.getList();
      this.getDicts("activity_coupon_type").then((response) => {
        this.couponTypeOptions = response.data;
      });
    },
    methods: {
      toCancelBox() {
        this.visible = false;
        this.$emit("toCloseBox2", false);
      },
      //接收父组件传过来的值，回显选中
      greet2(selectedData) {
        this.selectedDatas = selectedData;
        this.selectedDatas.forEach((item) => {
          this.$refs.indexTable.toggleRowSelection(item, true);
        });
      },
      bindRowKeys(row) {
        return row.couponUid;
      },
      handleSelectionChange(val) {
        this.datas = this.multipleSelection2;
        this.multipleSelection2 = val
        if (this.datas.length > 0) {
          for (let i = 0; i < this.datas.length; i++) {
            for (let k = 0; k < this.multipleSelection2.length; k++) {
              if (this.datas[i].couponUid == this.multipleSelection2[k].couponUid) {
                this.multipleSelection2[k] = this.datas[i]
              }
            }
          }
        }
      },
      determine() {
        //将数据传到父组件
        this.$emit("multipleSelection2", this.multipleSelection2);
        //清除选中状态
        this.infoList2.forEach((item) => {
          this.$refs.indexTable.toggleRowSelection(item, false);
        });
        this.$forceUpdate();
        this.open = false;
      },
      /** 查询优惠券信息列表 */
      getList() {
        this.loading = true;

        listCoupon(this.queryParams).then((response) => {
          this.infoList2 = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },

      // 类型字典翻译
      couponTypeFormat(row, column) {
        return this.selectDictLabel(this.couponTypeOptions, row.couponType);
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.queryParams.couponStatus = '1';
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
    }
  };
</script>

<style scoped>
  .input-rule {
    width: 232.45px;
  }

  .row-mb2 {
    margin: -7.5px -7.5px 7.5px -7.5px;
  }
</style>
